package com.czk.redis;

import com.alibaba.fastjson.JSON;
import com.czk.domain.MsgAgreement;
import com.czk.utils.CacheUtil;
import com.czk.utils.MsgUtil;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/4/29 11:23
 */
@Component
public class MsgAgreementReceiver extends AbstractReceiver {
    private static Logger LOGGER = LoggerFactory.getLogger(MsgAgreementReceiver.class);

    @Override
    public void receiveMessage(Object message) {
        LOGGER.info("接收到publish消息:{}" + message);
        // 转为对象
        MsgAgreement msgAgreement = JSON.parseObject(message.toString(), MsgAgreement.class);
        // 拿到channelId
        String channelId = msgAgreement.getToChannelId();
        // 拿到通道
        Channel channel = CacheUtil.cacheChannel.get(channelId);
        // 发送消息
        if (channel != null) {
            channel.writeAndFlush(MsgUtil.obj2Json(msgAgreement));
        }
    }
}
